package com.zzyl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.cache.UserCache;
import com.zzyl.dto.NursingLevelAddDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.entity.NursingPlan;
import com.zzyl.mapper.NursingLevelMapper;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelAllVo;
import com.zzyl.vo.NursingLevelVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 11010
* @description 针对表【nursing_level(护理等级表)】的数据库操作Service实现
* @createDate 2024-07-23 16:58:58
*/
@Service
public class NursingLevelServiceImpl implements NursingLevelService{

    private final NursingLevelMapper nursingLevelMapper;
    private final NursingPlanMapper nursingPlanMapper;

    public NursingLevelServiceImpl(NursingLevelMapper nursingLevelMapper, NursingPlanMapper nursingPlanMapper) {
        this.nursingLevelMapper = nursingLevelMapper;
        this.nursingPlanMapper = nursingPlanMapper;
    }

    @Override
    public List<NursingLevelAllVo> getAll() {
        List<NursingLevel> list = nursingLevelMapper.getAll();
        List<NursingPlan> nursingPlans = nursingPlanMapper.getByIds(list.stream().map(NursingLevel::getId).collect(Collectors.toList()));
        Map<Integer, String> map = nursingPlans.stream().collect(Collectors.toMap(NursingPlan::getId, NursingPlan::getPlanName));
        return list.stream().map((nursingLevel)->{
            NursingLevelAllVo nursingLevelAllVo = new NursingLevelAllVo();
            BeanUtils.copyProperties(nursingLevel,nursingLevelAllVo);
            nursingLevelAllVo.setPlanId(nursingLevel.getLplanId());
            nursingLevelAllVo.setCreator(nursingLevel.getCreateBy()==null?null:UserCache.localCache.get(nursingLevel.getCreateBy()));
            nursingLevelAllVo.setPlanName(map.get(nursingLevel.getLplanId()));
            return nursingLevelAllVo;
        }).collect(Collectors.toList());

    }

    @Override
    public void add(NursingLevelAddDto dto) {
        if (Objects.nonNull(nursingLevelMapper.getByName(dto.getName()))){
            throw new RuntimeException("该护理等级已存在");
        }
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(dto,nursingLevel);
        nursingLevelMapper.add(nursingLevel);
    }

    @Override
    public PageResponse<NursingLevelVo> getByPage(Integer pageNum, Integer pageSize, String name, Integer status) {
        Page<NursingLevel> page = PageHelper.startPage(pageNum, pageSize);
        List<NursingLevel> list = nursingLevelMapper.search(name, status);
        PageResponse<NursingLevelVo> response = new PageResponse<>();
        Map<Integer, String> map = nursingPlanMapper.getByIds(list.stream().map(NursingLevel::getId).collect(Collectors.toList()))
                .stream()
                .collect(Collectors.toMap(NursingPlan::getId, NursingPlan::getPlanName));
        List<NursingLevelVo> collect = page.getResult().stream().map((nursingLevel) -> {
            NursingLevelVo vo = new NursingLevelVo();
            BeanUtils.copyProperties(nursingLevel, vo);
            vo.setPlanId(nursingLevel.getLplanId());
            vo.setPlanName(map.get(nursingLevel.getLplanId()));
            vo.setCreator(UserCache.localCache.get(nursingLevel.getCreateBy()));
            return vo;
        }).collect(Collectors.toList());
        response.setPage(page.getPageNum());
        response.setPageSize(page.getPageSize());
        response.setPages((long) page.getPages());
        response.setTotal(page.getTotal());
        response.setRecords(collect);
        return response;
    }

    @Override
    public NursingLevelVo getById(Integer id) {
        NursingLevel nursingLevel = nursingLevelMapper.getById(id);
        if (Objects.isNull(nursingLevel)){
            throw new RuntimeException("该护理等级不存在");
        }
        NursingLevelVo vo = new NursingLevelVo();
        BeanUtils.copyProperties(nursingLevel,vo);
        vo.setPlanId(nursingLevel.getLplanId());
        vo.setPlanName(nursingPlanMapper.getById(nursingLevel.getLplanId()).getPlanName());
        vo.setCreator(UserCache.localCache.get(nursingLevel.getCreateBy()));
        return vo;
    }

    @Override
    public void update(NursingLevelAddDto dto) {
        NursingLevel byId = nursingLevelMapper.getById(dto.getId());
        if (Objects.isNull(byId)){
            throw new RuntimeException("该护理等级不存在");
        }
        if (!byId.getName().equals(dto.getName()) && Objects.nonNull(nursingLevelMapper.getByName(dto.getName()))){
            throw new RuntimeException("该护理等级已存在");
        }
        NursingLevel nursingLevel = new NursingLevel();
        BeanUtils.copyProperties(dto,nursingLevel);
        nursingLevel.setLplanId(dto.getPlanId());
        nursingLevelMapper.update(nursingLevel);
    }

    @Override
    public void delete(Integer id) {
        if (Objects.isNull(nursingLevelMapper.getById(id))){
            throw new RuntimeException("该护理等级不存在");
        }
        nursingLevelMapper.delete(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        if (Objects.isNull(nursingLevelMapper.getById(id))){
            throw new RuntimeException("该护理等级不存在");
        }
        NursingLevel nursingLevel = new NursingLevel();
        nursingLevel.setStatus(status);
        nursingLevel.setId(id);
        nursingLevelMapper.update(nursingLevel);
    }
}




